package jbase.project.system.core.service.beans;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import jbase.project.system.core.service.MethodContext;

public class MethodContextBean implements MethodContext {

	private Map<String, Object> params = new HashMap<String, Object>();

	public MethodContextBean() {

	}

	public MethodContextBean(Map<String, Object> initParams) {
		if (initParams != null) {
			params.putAll(initParams);
		} else {
			params = new HashMap<String, Object>();
		}
	}

	public void clear() {
		params.clear();
	}

	public boolean containsKey(Object key) {
		return params.containsKey(key);
	}

	public boolean containsValue(Object value) {
		return params.containsValue(value);
	}

	public Set<Entry<String, Object>> entrySet() {
		return params.entrySet();
	}

	public Object get(Object key) {
		return params.get(key);
	}

	public boolean isEmpty() {
		return params.isEmpty();
	}

	public Set<String> keySet() {
		return params.keySet();
	}

	public Object put(String key, Object value) {
		return params.put(key, value);
	}

	public void putAll(Map<? extends String, ? extends Object> m) {
		params.putAll(m);
	}

	public Object remove(Object key) {
		return params.remove(key);
	}

	public int size() {
		return params.size();
	}

	public Collection<Object> values() {
		return params.values();
	}

	@Override
	public Object getAsObject(String key) {
		return params.get(key);
	}

	@Override
	public Long getAsLong(String key) {
		Object value = params.get(key);
		if (value == null || value.equals("")) {
			return null;
		} else {
			Class<?> vClass = value.getClass();
			if (vClass == String.class) {
				return Long.valueOf((String) value);
			}
			if (vClass == Integer.class || vClass == int.class) {
				return Long.valueOf((Integer) value);
			}
			if (vClass == Long.class || vClass == long.class) {
				return (Long) value;
			}
			if (vClass == Float.class || vClass == float.class) {
				Float floatValue = (Float) value;
				long longValue = (long) floatValue.floatValue();
				if (longValue == floatValue.floatValue()) {
					return longValue;
				}
			}
			if (vClass == Double.class || vClass == double.class) {
				Double doubleValue = (Double) value;
				long longValue = (long) doubleValue.doubleValue();
				if (longValue == doubleValue.doubleValue()) {
					return longValue;
				}
			}
			if (vClass == Boolean.class || vClass == boolean.class) {
				if ((Boolean) value) {
					return 1L;
				} else {
					return 0L;
				}
			}
			throw new RuntimeException("无法转换类型！");
		}
	}

	@Override
	public Integer getAsInt(String key) {
		Object value = params.get(key);
		if (value == null || value.equals("")) {
			return null;
		} else {
			Class<?> vClass = value.getClass();
			if (vClass == String.class) {
				return Integer.valueOf((String) value);
			}
			if (vClass == Integer.class || vClass == int.class) {
				return (Integer) value;
			}
			if (vClass == Long.class || vClass == long.class) {
				return ((Long) value).intValue();
			}
			if (vClass == Float.class || vClass == float.class) {
				Float floatValue = (Float) value;
				int intValue = (int) floatValue.floatValue();
				if (intValue == floatValue.floatValue()) {
					return intValue;
				}
			}
			if (vClass == Double.class || vClass == double.class) {
				Double doubleValue = (Double) value;
				int intValue = (int) doubleValue.doubleValue();
				if (intValue == doubleValue.doubleValue()) {
					return intValue;
				}
			}
			if (vClass == Boolean.class || vClass == boolean.class) {
				if ((Boolean) value) {
					return 1;
				} else {
					return 0;
				}
			}
			throw new RuntimeException("无法转换类型！");
		}
	}

	@Override
	public Boolean getAsBoolean(String key) {
		Object value = params.get(key);
		if (value == null || value.equals("")) {
			return null;
		} else {
			Class<?> vClass = value.getClass();
			if (vClass == String.class) {
				String strValue = (String) value;
				if (strValue.equalsIgnoreCase("Y") || strValue.equalsIgnoreCase("YES")
						|| strValue.equalsIgnoreCase("TRUE") || strValue.equalsIgnoreCase("T")) {
					return true;
				}
				if (strValue.equalsIgnoreCase("N") || strValue.equalsIgnoreCase("NO")
						|| strValue.equalsIgnoreCase("FALSE") || strValue.equalsIgnoreCase("F")) {
					return false;
				}
			}
			if (vClass == Integer.class || vClass == int.class) {
				Integer intValue = (Integer) value;
				if (intValue.intValue() == 1) {
					return true;
				}
				if (intValue.intValue() == 0) {
					return false;
				}
			}
			if (vClass == Long.class || vClass == long.class) {
				Long longValue = (Long) value;
				if (longValue.longValue() == 1L) {
					return true;
				}
				if (longValue.longValue() == 0L) {
					return false;
				}
			}
			if (vClass == Float.class || vClass == float.class) {
				Float floatValue = (Float) value;
				if (floatValue.floatValue() == 1F) {
					return true;
				}
				if (floatValue.floatValue() == 0F) {
					return false;
				}
			}
			if (vClass == Double.class || vClass == double.class) {
				Double dbValue = (Double) value;
				if (dbValue.doubleValue() == 1D) {
					return true;
				}
				if (dbValue.doubleValue() == 0D) {
					return false;
				}
			}
			if (vClass == Boolean.class || vClass == boolean.class) {
				return (Boolean) value;
			}
			throw new RuntimeException("无法转换类型！");
		}
	}

	@Override
	public Float getAsFloat(String key) {
		Object value = params.get(key);
		if (value == null || value.equals("")) {
			return null;
		} else {
			Class<?> vClass = value.getClass();
			if (vClass == String.class) {
				return Float.valueOf((String) value);
			}
			if (vClass == Integer.class || vClass == int.class) {
				return Float.valueOf((Integer) value);
			}
			if (vClass == Long.class || vClass == long.class) {
				return Float.valueOf((Long) value);
			}
			if (vClass == Float.class || vClass == float.class) {
				return (Float) value;
			}
			if (vClass == Double.class || vClass == double.class) {
				Double doubleValue = (Double) value;
				float floatValue = (float) doubleValue.doubleValue();
				if (floatValue == doubleValue.doubleValue()) {
					return floatValue;
				}
			}
			if (vClass == Boolean.class || vClass == boolean.class) {
				if ((Boolean) value) {
					return 1F;
				} else {
					return 0F;
				}
			}
			throw new RuntimeException("无法转换类型！");
		}
	}

	@Override
	public Double getAsDouble(String key) {
		Object value = params.get(key);
		if (value == null || value.equals("")) {
			return null;
		} else {
			Class<?> vClass = value.getClass();
			if (vClass == String.class) {
				return Double.valueOf((String) value);
			}
			if (vClass == Integer.class || vClass == int.class) {
				return Double.valueOf(((Integer) value));
			}
			if (vClass == Long.class || vClass == long.class) {
				return Double.valueOf(((Long) value));
			}
			if (vClass == Float.class || vClass == float.class) {
				return Double.valueOf((Float) value);
			}
			if (vClass == Double.class || vClass == double.class) {
				return (Double) value;
			}
			if (vClass == Boolean.class || vClass == boolean.class) {
				if ((Boolean) value) {
					return 1D;
				} else {
					return 0D;
				}
			}
			throw new RuntimeException("无法转换类型！");
		}
	}

	@Override
	public String getAsString(String key) {
		Object value = params.get(key);
		if (value == null) {
			return null;
		} else {
			Class<?> vClass = value.getClass();
			if (vClass == String.class) {
				return (String) value;
			}
			if (vClass == Integer.class || vClass == int.class) {
				return String.valueOf((Integer) value);
			}
			if (vClass == Long.class || vClass == long.class) {
				return String.valueOf((Long) value);
			}
			if (vClass == Float.class || vClass == float.class) {
				return String.valueOf((Float) value);
			}
			if (vClass == Double.class || vClass == double.class) {
				return String.valueOf((Double) value);
			}
			if (vClass == Boolean.class || vClass == boolean.class) {
				if ((Boolean) value) {
					return "true";
				} else {
					return "false";
				}
			}
			throw new RuntimeException("无法转换类型！");
		}
	}

	@Override
	public String toString() {
		return params.toString();
	}

}
